package net.cjw.initsb.controller.background;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.entity.Post;
import net.cjw.initsb.service.PostServiceI;
@Api(description="帖子")
@RestController
@RequestMapping("topic_post")
public class TopicPostController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PostServiceI postServiceI;

	@PostMapping("v{version}/create_imgs")
	public ServiceResult<String> createImgs(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "主题帖子标题", defaultValue = "我是帖子标题") @RequestParam String title,
			@ApiParam(value = "图片url,多个逗号隔开,最多9张", defaultValue = "xxx.jpg,xxx.jpg") @RequestParam String image_url,
			@ApiParam(value = "帖子内容", defaultValue = "我是帖子内容") @RequestParam String content,
			@ApiParam(value = "帖子类型 image user_feedback", defaultValue = "image") @RequestParam String post_type,
			@ApiParam(value = "帖子状态 0 删除 1 审核中 2 审核通过 ", defaultValue = "1") @RequestParam Integer post_sate) {

		String uid = "admin";
		if (Strings.isNullOrEmpty(image_url) || Strings.isNullOrEmpty(content) || Strings.isNullOrEmpty(post_type)
				|| post_sate == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Post topicPost = new Post();
			topicPost.setContent(content);
			topicPost.setTitle(title);
			topicPost.setImageUrl(image_url);
			topicPost.setPostType(post_type);
			topicPost.setPostSate(post_sate);
			topicPost.setUid(uid);
			return postServiceI.create(topicPost);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/create_video")
	public ServiceResult<String> createVideo(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "帖子标题", defaultValue = "我是标题") @RequestParam String title,
			@ApiParam(value = "视频url多个逗号隔开", defaultValue = "xxx.mp4") @RequestParam String video_url,
			@ApiParam(value = "视频自定义封面", defaultValue = "xxx.jpg") @RequestParam String video_cover_url,
			@ApiParam(value = "帖子内容", defaultValue = "我是帖子内容") @RequestParam String content,
			@ApiParam(value = "帖子类型 video", defaultValue = "video") @RequestParam String post_type,
			@ApiParam(value = "帖子状态 0 删除 1 审核中 2 审核通过 ", defaultValue = "1") @RequestParam Integer post_sate) {
		String uid = "admin";
		if (Strings.isNullOrEmpty(video_url) || Strings.isNullOrEmpty(video_cover_url) || Strings.isNullOrEmpty(content)
				|| Strings.isNullOrEmpty(post_type) || post_sate == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Post post = new Post();
			post.setContent(content);
			post.setPostSate(post_sate);
			post.setTitle(title);
			post.setPostType(post_type);
			post.setUid(uid);
			post.setVideoCoverUrl(video_cover_url);
			post.setVideoUrl(video_url);
			return postServiceI.create(post);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/update")
	public ServiceResult<String> update(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "帖子id", defaultValue = "") @RequestParam String id,
			@ApiParam(value = "帖子状态 0 删除 1 审核中 2 审核通过", defaultValue = "1") @RequestParam Integer post_sate) {
		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Post post = new Post();
			post.setId(id);
			post.setPostSate(post_sate);
			return postServiceI.update(post);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/select_all")
	public ServiceResult<Object> selectAll(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "页数", defaultValue = "0") @RequestParam Integer page_num,
			@ApiParam(value = "结果数", defaultValue = "10") @RequestParam Integer max_result) {
		if (page_num == null || max_result == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			return postServiceI.selectAll(page_num, max_result);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
